package com.zhengqing.system.api;


import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhengqing.common.auth.model.dto.AuthLoginDTO;
import com.zhengqing.common.auth.model.vo.AuthLoginVO;
import com.zhengqing.common.auth.service.IAuthService;
import com.zhengqing.system.entity.ManualCatalogOm;
import com.zhengqing.system.model.dto.YoutubeGetMediaDto;
import com.zhengqing.system.service.IManualCatalogOmService;
import com.zhengqing.system.service.IManualModelOmService;
import com.zhengqing.system.service.YoutubeDownloadService;
import com.zhengqing.system.util.MinIoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p> 授权认证 </p>
 *
 * @author zhengqingya
 * @description
 * @date 2021/10/5 2:36 下午
 */
@Slf4j
@RestController
@RequestMapping("/auth")
@Api(tags = "授权认证api")
@RequiredArgsConstructor
public class AuthController {

    private final IAuthService authService;

    @Resource
    private IManualModelOmService manualModelOmService;

    @Resource
    IManualCatalogOmService manualCatalogOmService;

    @Resource
    YoutubeDownloadService youtubeDownloadService;

    @Autowired
    HttpServletRequest request;


    @PostMapping("login")
    @ApiOperation("登录")
    public AuthLoginVO login(@Validated @RequestBody AuthLoginDTO params) {
        return this.authService.login(params);
    }

    @DeleteMapping("logout")
    @ApiOperation("注销登录")
    public void logout() {
        StpUtil.logout();
    }


    @GetMapping("/downloadCatalog")
    public String downloadCatalog(){
        manualModelOmService.downloadManualPdfs();
        return "success";
    }

    @GetMapping("/uploadManualPdfToMinio")
    public String uploadManualPdfToMinio(){
        manualModelOmService.uploadManualPdfToMinio();
        return "success";
    }

    @GetMapping("/createThumbnailToMinio")
    public String createThumbnailToMinio(){
        manualModelOmService.createThumbnailToMinio();
        return "success";
    }

    @PostMapping("/youtubeMediaGet")
    public String youtubeMediaGet(@RequestBody YoutubeGetMediaDto param){
        log.debug("Host->{}", JSONObject.toJSONString(request.getHeaderNames()));
//        log.debug("Content-Length->{}",request.getHeader("Content-Length"));
        youtubeDownloadService.downloadProcess(param);
        return "success";
    }
//
//    @GetMapping("/test")
//    public String test(MultipartFile file,String name){
//        ManualCatalogOm res = manualCatalogOmService.getOne(Wrappers.<ManualCatalogOm>lambdaQuery().eq(ManualCatalogOm::getModelId, 4049L));
//        String url = null;
//        try {
//            url = MinIoUtil.getObjectUrl("manuals",res.getCatalogMinioPath(),60);
//            System.out.println(123);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return url;
//    }

}
